package com.javaweb.web.controller;

import java.util.Date;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javaweb.annotation.token.TokenDataAnnotation;
import com.javaweb.annotation.url.ControllerMethod;
import com.javaweb.base.BaseResponseResult;
import com.javaweb.base.BaseService;
import com.javaweb.base.po.Dictionary;
import com.javaweb.base.po.User;
import com.javaweb.constant.ApiCommonConstant.DictionaryConstant;
import com.javaweb.constant.CommonConstant;
import com.javaweb.constant.SystemConstant;
import com.javaweb.constant.SystemConstant.RedisConstant;
import com.javaweb.enums.AuthEnum;
import com.javaweb.enums.HttpCodeEnum;
import com.javaweb.util.entity.Page;
import com.javaweb.web.eo.TokenData;
import com.javaweb.web.eo.dictionary.DictionaryListRequest;

/**
 * 登录且需要权限才可访问的字典管理模块
 * <p>
 * 该模块主要提供字典管理相关的接口
 * 
 * @author tank2140896
 */
@RestController
@RequestMapping(DictionaryConstant.WEB_DICTIONARY_PREFIX)
public class DictionaryController extends BaseService {
    
	/**
	 * 新增字典接口
	 * 
	 * @param dictionary 字典新增请求参数
	 * @param tokenData tokenData
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PostMapping(DictionaryConstant.DICTIONARY_ADD)
	@ControllerMethod(interfaceName="新增字典接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult dictionaryAdd(@RequestBody @Validated Dictionary dictionary,@TokenDataAnnotation TokenData tokenData){
		User currentUser = tokenData.getUser();
		var date = new Date();
		dictionary.setSort(CommonConstant.ZERO_STRING_VALUE);
		dictionary.setUniversally(0);
		dictionary.setSystemId(SystemConstant.SYSTEM_NO);
		dictionary.setCreator(currentUser.getUserId());
		dictionary.setCreateDate(date);
		dictionary.setUpdater(currentUser.getUserId());
		dictionary.setUpdateDate(date);
		dictionaryService.dictionaryAdd(dictionary);
		stringRedisTemplate.convertAndSend(RedisConstant.REDIS_DICTIONARY_TOPIC,RedisConstant.REDIS_DICTIONARY_TOPIC);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"dictionary.add.success");
	}
	
	/**
	 * 查询字典接口
	 * 
	 * @param dictionaryListRequest 字典列表请求参数
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PostMapping(DictionaryConstant.DICTIONARY_LIST)
	@ControllerMethod(interfaceName="查询字典接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult dictionaryList(@RequestBody DictionaryListRequest dictionaryListRequest){
		Page page = dictionaryService.dictionaryList(dictionaryListRequest);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"dictionary.list.success",page);
	}
	
	/**
	 * 修改字典接口
	 * 
	 * @param dictionary 字典修改请求参数
	 * @param tokenData tokenData
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PutMapping(DictionaryConstant.DICTIONARY_MODIFY)
	@ControllerMethod(interfaceName="修改字典接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult dictionaryModify(@RequestBody @Validated Dictionary dictionary,@TokenDataAnnotation TokenData tokenData){
		User currentUser = tokenData.getUser();
		dictionary.setUpdater(currentUser.getUserId());
		dictionary.setUpdateDate(new Date());
		dictionaryService.dictionaryModify(dictionary);
		stringRedisTemplate.convertAndSend(RedisConstant.REDIS_DICTIONARY_TOPIC,RedisConstant.REDIS_DICTIONARY_TOPIC);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"dictionary.modify.success");
	}
	
	/**
	 * 字典详情接口
	 * 
	 * @param dictionaryId 字典ID
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@GetMapping(DictionaryConstant.DICTIONARY_DETAIL)
	@ControllerMethod(interfaceName="字典详情接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult dictionaryDetail(@PathVariable(name="dictionaryId",required=true) String dictionaryId){
		Dictionary dictionary = dictionaryService.dictionaryDetail(dictionaryId);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"dictionary.detail.success",dictionary);
	}
	
	/**
	 * 删除字典接口（支持批量删除，用逗号隔开）
	 * 
	 * @param dictionaryId 字典ID
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@DeleteMapping(DictionaryConstant.DICTIONARY_DELETE)
	@ControllerMethod(interfaceName="删除字典接口（支持批量删除，用逗号隔开）",auth=AuthEnum.PERMISSION)
	public BaseResponseResult dictionaryDelete(@PathVariable(name="dictionaryId",required=true) String dictionaryId){
		dictionaryService.dictionaryDelete(dictionaryId);
		stringRedisTemplate.convertAndSend(RedisConstant.REDIS_DICTIONARY_TOPIC,RedisConstant.REDIS_DICTIONARY_TOPIC);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"dictionary.delete.success");
	}
	
}
